<?php
	ini_set('display_errors', true);
	require_once(dirname(__FILE__) . "/../model/db_driver.php");
	session_start();
	if(User::getCurrentUser()==null || User::getCurrentUser()->getRuolo()<User::$SA){
		echo "Errore: devi essere un super amministratore per visualizzare questa pagina.<br />";
		die();
	}
	if(isset($_POST['txtQuery'])){
		$query = $_POST['txtQuery'];
		$db = new DbDriver();
		if(isset($_POST['btnEsegui'])){
			echo $db->query_result($query);
		}elseif(isset($_POST['btnAggiorna'])){
			try{
				$db->query_update($query);
				echo "<p style='color:blue;'>Query eseguita con successo</p>";
			}catch(Exception $ex){
				echo "<p style='color:red;'>Errore nella query:<p>
					<div>
				" . $ex->getMessage() . "</div>";
					
			}
			
		}
		die();
	}
	



?>


	<style>
	
		td.cellHeaderResult{
			border-style: solid;
			border-width:1px;
			border-color:black;
			text-align:center;
			font-weight:bold;
			padding-left:3px;
			padding-right:3px;
			color:blue;
			background-color:#FFE7A1;
		}
		
		td.cellResult{
			border-style: solid;
			border-width:1px;
			border-color: black;
			padding-left:5px;
			padding-right:3px;
			color:black;
		}
		
		td.alt_0{
			background-color:#3399FF;
		}
		
		td.alt_1{
			background-color:#A2ABC0;
		}
		
		td.headerRow {
			background-color:rgb(187,204,255);
			color:rgb(23,128,128);
			font-size:15px;
			border-right: solid 1px rgb(23,128,128);
			border-top: solid 1px rgb(23,128,128);
		
			padding-left: 3px;
			padding-right:3px;
			font-weight:bold;
		}
		
		td.rigaUtente  {
			color:black;
			font-size:12px;
			border-right: solid 1px rgb(23,128,128);
			border-top: solid 1px rgb(23,128,128);
			padding-left: 3px;
			padding-right:3px;
			
		}

		td.alt_0{
			background-color:rgb(207,219,236);
		}
		
		td.alt_1{
			background-color:rgb(227,233,255);		
		}
		
		td.details{
			border-bottom:ridge 1px gray;
			border-right:ridge 1px gray;
			padding-bottom:3px;
			padding-left:3px;
			padding-top:3px;
		}
		
		td.ldetails{
			width:20%;
			text-align:right;
			padding-right:5px;
		}
		
		input.txt{
			width:95%;
			font-size:10px;
		}

			
		select.combo{
			font-size:10px;
			width:95%;
		}
	</style>
	<p  class='adminTitle'>
		Amministrazione database
	</p>
	<table style='width:100%'>
		<tr>
			<td style='width:33%;text-align:center;'>
				<button id='btnExecute' onclick='execQuery()'>Esegui query</button>
			</td>
			<td style='width:33%;text-align:center;'>
				<button id='btnAggiorna' onclick='execNonQuery()'>Aggiorna DB</button>
			</td>
			<td style='width:33%;text-align:center;'>
				<button id='viewSchema' onclick='viewSchema()'>Visualizza schema</button>
			</td>		
		</tr>
		<tr>
			<td colspan="3"  style='width:100%'>
				<textarea id='txtQuery' name='txtQuery' style='width:700px; height:200px'></textarea>
			</td>
		</tr>
		<tr>
			<td colspan="3" style='width:100%'>
				<div id='dvQueryResult' style='width:700px;height:300px;overflow:scroll;border:solid 1px gray;'></div>
			</td>
		</tr>
	</table>
	<br />
	<div id='dvSchema' style='background-color:#FAFAD2;display:none;border: solid 1px black;
		position:absolute;top:100px;left:100px;	background-image:url(../images/move.png);
		background-position-x:0px;background-position-y:0px;background-repeat:no-repeat;'
			onmousemove='movingDiv()'
			onmousedown=' moveDiv()'
			onmouseout='stopMovDiv()'
			onmouseup='stopMoveDiv();' >
		<br />
		<img src='../images/SchemaDB_Leghe.jpg' alt='schema db'
			onclick='document.getElementById("dvSchema").style.display="none";'
			onmousedown=' moveDiv()'
			onmouseup='stopMoveDiv();' 
			/>
		
	</div>

